package com.example.fableoid;



import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Descriptor extends ListActivity {
	
	private GestureDetector gestureDetector;
	@SuppressWarnings("deprecation")
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.descriptor);
		
        gestureDetector = new GestureDetector(new SwipeGestureDetector());
        
		ListView listView = (ListView) findViewById(android.R.id.list);
		 // storing string resources into Array
       String[] story_titles = getResources().getStringArray(R.array.story_list);

       // Binding resources Array to ListAdapter
       
       ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1,story_titles);
       
       listView.setAdapter(adapter);
       
       //set single choice of list at a time
       listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
       
       
       //setting the story description when item clicked from selector class
       
       TextView story_desc = (TextView) findViewById(R.id.story_desc);
       
       TextView story_title = (TextView)findViewById(R.id.textView2);
       
       ImageView image_desc = (ImageView) findViewById(R.id.imageView1);
       
       
       Intent i = getIntent();
       
       String title = i.getStringExtra("title");
       
       if(title.equalsIgnoreCase("The Ant and the Grasshopper")){
    	   story_title.setText(title);
       story_desc.setText(R.string.ant_desc);
       image_desc.setImageResource(R.raw.ant_and_grasshopper);
       }
       
       if(title.equalsIgnoreCase("The Fox and the Grapes")) {
    	   story_title.setText(title);
    	   story_desc.setText(R.string.fox_desc);
           image_desc.setImageResource(R.raw.fox);
       }
       
       if(title.equalsIgnoreCase("The Wind and the Sun")){
    	   story_title.setText(title);
    	   story_desc.setText(R.string.wind_desc);
    	   image_desc.setImageResource(R.raw.wind_and_the_sun);
       }
       
       if(title.equalsIgnoreCase("The Miser and his Gold")){   
    	   story_title.setText(title);
    	   story_desc.setText(R.string.miser_desc);
           image_desc.setImageResource(R.raw.miser);
       }
       
       if(title.equalsIgnoreCase("The Frog and the Ox")){
    	   story_title.setText(title);
    	   story_desc.setText(R.string.frog_desc);
    	   image_desc.setImageResource(R.raw.frog_and_ox);
       }
           
	}
	
	public void onListItemClick(ListView parent,View view, int position, long id) {
		String title = ((TextView) view).getText().toString();
		
		TextView story_title = (TextView)findViewById(R.id.textView2);
		
		TextView story_desc = (TextView) findViewById(R.id.story_desc);
		
		ImageView image_desc = (ImageView) findViewById(R.id.imageView1);
		
		if(title.equalsIgnoreCase("The Ant and the Grasshopper")){
		       story_title.setText(title);
			   story_desc.setText(R.string.ant_desc);
		       image_desc.setImageResource(R.raw.ant_and_grasshopper);
		       }
		       
		       if(title.equalsIgnoreCase("The Fox and the Grapes")) {
		    	   story_title.setText(title);
		    	   story_desc.setText(R.string.fox_desc);
		           image_desc.setImageResource(R.raw.fox);
		       }
		       
		       if(title.equalsIgnoreCase("The Wind and the Sun")){
		    	   story_title.setText(title);
		    	   story_desc.setText(R.string.wind_desc);
		    	   image_desc.setImageResource(R.raw.wind_and_the_sun);
		       }
		       
		       if(title.equalsIgnoreCase("The Miser and his Gold")){   
		    	   story_title.setText(title);
		    	   story_desc.setText(R.string.miser_desc);
		           image_desc.setImageResource(R.raw.miser);
		       }
		       
		       if(title.equalsIgnoreCase("The Frog and the Ox")){
		    	   story_title.setText(title);
		    	   story_desc.setText(R.string.frog_desc);
		    	   image_desc.setImageResource(R.raw.frog_and_ox);
		       }
		    	
		
	}
	
	public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
          return true;
        }
        return super.onTouchEvent(event);
      }
    
    private void onLeftSwipe() {
    
       TextView story_title = (TextView) findViewById(R.id.textView2);
       
       Intent intent = new Intent(this,Story.class);
       intent.putExtra("title", story_title.getText());
       startActivity(intent);
      }

      private void onRightSwipe() {
        // Do something
      }
      
	
	private class SwipeGestureDetector extends SimpleOnGestureListener {

		private static final int SWIPE_MIN_DISTANCE = 120;
		private static final int SWIPE_MAX_OFF_PATH = 200;
		private static final int SWIPE_THRESHOLD_VELOCITY = 200;

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2,
		     float velocityX, float velocityY) {
		  try {
		    float diffAbs = Math.abs(e1.getY() - e2.getY());
		    float diff = e1.getX() - e2.getX();

		    if (diffAbs > SWIPE_MAX_OFF_PATH)
		      return false;
		    
		    // Left swipe
		    if (diff > SWIPE_MIN_DISTANCE
		    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
		       Descriptor.this.onLeftSwipe();

		    // Right swipe
		    } else if (-diff > SWIPE_MIN_DISTANCE
		    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
		      Descriptor.this.onRightSwipe();
		    }
		  } catch (Exception e) {
		    Log.e("YourActivity", "Error on gestures");
		  }
		  return false;
		}
		} 
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
